﻿/****************************** Module Header ******************************\
 * Module Name:  ListItemTable.cs
 * Module Version:  1.0.0 (2012-8-25)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * This class is used to list the file type.
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/
using System;
using System.Collections.Generic;
using System.Data;

namespace UCF.Core
{
    /// <summary>
    /// 列表键值表格对象,用于ComboBox控件的数据源,[0]=ID,[1]=Name
    /// <para>comboBox1.DataSource = ListItemTable.New&lt;int, string&gt;("level").Add(1, "普通客户").Add(2, "银牌客户").Add(3, "金牌客户").ToTable();</para>
    /// <para>comboBox1.DisplayMember = "Name";</para>
    /// <para>comboBox1.ValueMember = "ID";</para>
    /// </summary>
    /// <typeparam name="K"></typeparam>
    /// <typeparam name="V"></typeparam>
    public class ListItemTable
    {
        private DataTable dataTable;
        private static ListItemTable instance;

        /// <summary>
        /// 创建一个新对象
        /// </summary>
        /// <typeparam name="V1"></typeparam>
        /// <typeparam name="V2"></typeparam>
        /// <param name="colName1"></param>
        /// <param name="colName2"></param>
        /// <returns></returns>
        public static ListItemTable New<V1, V2>(string colName1 = "ID", string colName2 = "Name")
        {
            instance = new ListItemTable();
            instance.dataTable = new DataTable();
            instance.dataTable.Columns.Add(colName1, typeof(V1));
            instance.dataTable.Columns.Add(colName2, typeof(V2));
            return instance;
        }
        public static ListItemTable New<V1, V2, V3>(string colName1 = "col1", string colName2 = "col2", string colName3 = "col3")
        {
            instance = new ListItemTable();
            instance.dataTable = new DataTable();
            instance.dataTable.Columns.Add(colName1, typeof(V1));
            instance.dataTable.Columns.Add(colName2, typeof(V2));
            instance.dataTable.Columns.Add(colName3, typeof(V3));
            return instance;
        }
        public static ListItemTable New<V1, V2, V3, V4>(string colName1="col1", string colName2="col2", string colName3="col3", string colName4="col4")
        {
            instance = new ListItemTable();
            instance.dataTable = new DataTable();
            instance.dataTable.Columns.Add(colName1, typeof(V1));
            instance.dataTable.Columns.Add(colName2, typeof(V2));
            instance.dataTable.Columns.Add(colName3, typeof(V3));
            instance.dataTable.Columns.Add(colName4, typeof(V4));
            return instance;
        }
        public static ListItemTable New<V1, V2, V3, V4, V5>(string colName1 = "col1", string colName2 = "col2", string colName3 = "col3", string colName4 = "col4",string colName5="col5")
        {
            instance = new ListItemTable();
            instance.dataTable = new DataTable();
            instance.dataTable.Columns.Add(colName1, typeof(V1));
            instance.dataTable.Columns.Add(colName2, typeof(V2));
            instance.dataTable.Columns.Add(colName3, typeof(V3));
            instance.dataTable.Columns.Add(colName4, typeof(V4));
            instance.dataTable.Columns.Add(colName5, typeof(V5));
            return instance;
        }
        /// <summary>
        /// 初始化列表键值表格对象
        /// </summary>
        /// <param name="k"></param>
        /// <param name="v"></param>
        /// <returns></returns>
        public ListItemTable Add<V, T>(V k, T v)
        {
            dataTable.Rows.Add(k, v);
            return instance;
        }
        public ListItemTable Add<V1, V2, V3>(V1 v1, V2 v2, V3 v3)
        {
            dataTable.Rows.Add(v1, v2, v3);
            return instance;
        }
        public ListItemTable Add<V1, V2, V3, V4>(V1 v1, V2 v2, V3 v3, V4 v4)
        {
            dataTable.Rows.Add(v1, v2, v3, v4);
            return instance;
        }
        public ListItemTable Add<V1, V2, V3, V4, V5>(V1 v1, V2 v2, V3 v3, V4 v4, V5 v5)
        {
            dataTable.Rows.Add(v1, v2, v3, v4, v5);
            return instance;
        }
        public DataTable ToTable()
        {
            instance = null;
            return dataTable;
        }
    }
}
